<?php
class ets extends transactionBase {
	private $clientID;
    private $masterKey;
    private $vendorID;
    private $vendorName;
    private $vendorProduct;
    protected $postUrl;
    protected $postParams;
    protected $postResponse;
	protected $allowedTransactionTypes = array("AUTHORIZE_ONLINE", "CREDIT_TRANSACTION", "VOID_TRANSACTION", "RETRIEVE_TRANSACTION");

	public function __construct() {
        $this->postUrl = "https://pos.etsemoney.com/ECOM3X/main.asp";
        $this->transactionType = "AUTHORIZE_ONLINE";
	}

	public function authenticate($clientID, $masterKey, $vendorID="", $vendorName="", $vendorProduct="") {
		$this->clientID = $clientID;
		$this->masterKey = $masterKey;
		$this->vendorID = $vendorID;
		$this->vendorName = $vendorName;
		$this->vendorProduct = $vendorProduct;
	}

	private function getPostString() {
        $str = "";
		$str .= "\n" . '<?xml version="1.0" encoding="ISO-8859-1"?>'; 
        $str .= "\n<RequestMessage>"; 
        $str .= "\n\t<Action>" . $this->transactionType . "</Action>"; 
        $str .= "\n\t<ClientID>" . $this->clientID . "</ClientID>"; 
        $str .= "\n\t<MasterKey>" . $this->masterKey . "</MasterKey>"; 
        $str .= "\n\t<AccountNumber>" . $this->cardnumber . "</AccountNumber>"; 
        $str .= "\n\t<VendorID>" . $this->vendorID . "</VendorID>"; 
        $str .= "\n\t<VendorName>" . $this->vendorName . "</VendorName>"; 
        $str .= "\n\t<VendorProduct>" . $this->vendorProduct . "</VendorProduct>"; 
        $str .= "\n\t<PurchaseIdentifier1>" . $this->description . "</PurchaseIdentifier1>"; 
        if($this->transactionType == "RETRIEVE_TRANSACTION")
            $str .= "\n\t<SettlementAmount>" . $this->amount . "</SettlementAmount>"; 
        else
        {
            $str .= "\n\t<TransactionAmount>" . $this->amount . "</TransactionAmount>"; 
            $str .= "\n\t<CVV>" . $this->cvv . "</CVV>"; 
            $str .= "\n\t<ExpirationDate>" . $this->expiration . "</ExpirationDate>"; 
            $str .= "\n\t<NameOnCard>" . $this->fname . " " . $this->lname . "</NameOnCard>"; 
            $str .= "\n\t<AVSStreet1>" . $this->address . "</AVSStreet1>"; 
            $str .= "\n\t<AVSStreet2></AVSStreet2>"; 
            $str .= "\n\t<AVSZip>" . $this->zip . "</AVSZip>"; 
        }
        $str .= "\n</RequestMessage>";
        return $str;
    }

	private function parseResponse() {
        $xmlObj = simplexml_load_string($this->postResponse);

		$responseCode = trim($xmlObj->ResponseCode);
		$responseText = trim($xmlObj->ResponseText);
		$avsResultCode = trim($xmlObj->AVSResultCode);
		$cvvResultCode = trim($xmlObj->CVVResultCode);
		$this->transactionID = trim($xmlObj->TransactionID);

		//These lines allow the request/response xml to be displayed more easily in a browser.
		$this->postResponse = "\n" . htmlentities($this->postResponse);
		$this->postParams = htmlentities($this->postParams);

        if($responseCode == "00" || $responseCode == "85")
            return true;
        else {
			if($responseCode[0] == "Z")
				$this->errorMessage = $responseText;
			elseif($cvvResultCode != "M")
                $this->errorMessage = "A CVV2 (Card Verification Value) error was encountered.  Please check the secuity code provided and re-try.";
			elseif($responseCode == "54")
                $this->errorMessage = "The expiration date provided was invalid.  Please verify that the expiration date provided is not expired and matches what appears on the credit card.";
			elseif(in_array($avsResultCode, array("A", "W", "Z", "N", "U", "R", "E", "S")))
                $this->errorMessage = "An AVS (Address Verification Service) error was encountered.  Please check the address and zip/postal code provided and re-try.";

			if(empty($this->errorMessage))
				$this->errorMessage = "The credit card transaction was declined.  Please check your billing informaiton and re-try.";
            return false;
		}	
    }

	public function process() {
		$this->postParams = $this->getPostString();
		$request = curl_init($this->postUrl);
		curl_setopt($request, CURLOPT_HEADER, 0);
		curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($request, CURLOPT_POSTFIELDS, $this->postParams);
        curl_setopt($request, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
		$this->postResponse = curl_exec($request);
		curl_close($request);
		return $this->parseResponse();
	}

	public function setTestMode() {
		$this->postUrl = "https://demo.etsemoney.com/ECOM3X/main.asp";
	}
}
?>
